# Maintainer: MVZ Labor Ludwigsburg <edv@mvz-labor-lb.de>

_realname=mupdf
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-libmupdf"
  "${MINGW_PACKAGE_PREFIX}-mupdf"
  "${MINGW_PACKAGE_PREFIX}-mupdf-gl"
  "${MINGW_PACKAGE_PREFIX}-mupdf-tools"
)
pkgver=1.24.3
pkgrel=2
pkgdesc='Lightweight PDF and XPS viewer (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://mupdf.com/'
msys2_repository_url='https://github.com/ArtifexSoftware/mupdf/'
msys2_documentation_url='https://mupdf.readthedocs.io/'
msys2_references=(
  'archlinux: mupdf'
  "cpe: cpe:/a:artifex:mupdf"
)
license=('spdx:AGPL-3.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-freetype"
  "${MINGW_PACKAGE_PREFIX}-harfbuzz"
  "${MINGW_PACKAGE_PREFIX}-jbig2dec"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-openjpeg2"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-gumbo-parser"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-freeglut"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "lzip"
  "unzip"
)
source=("https://mupdf.com/downloads/archive/${_realname}-${pkgver}-source.tar.lz"
        "001-makerules-search-system-libraries.patch"
        "002-makerules-fix-gl-libraries.patch"
)
noextract=("${_realname}-${pkgver}-source.tar.lz")
sha256sums=('656fbea2210e9720d69bd199fc85b19d70ece2a3c91d85f5d5d2147ebea42db3'
            'ab93d8d1ff69752ef897fa56134d17d8dda1ea19ee7a9c368090ed7effb3101c'
            '082b0fd6f162b91ff2bc263e24440ad7297e1ab5db7e1e2d76f1f0d5215af8ad')

prepare() {
  tar -xf "${_realname}-${pkgver}-source.tar.lz" || true

  cd "${_realname}-${pkgver}-source"

  # remove bundled packages, we want our system libraries
  rm -rf thirdparty/{freeglut,freetype,harfbuzz,jbig2dec,libjpeg,openjpeg,zlib}

  patch -p1 -i "${srcdir}/001-makerules-search-system-libraries.patch"
  patch -p1 -i "${srcdir}/002-makerules-fix-gl-libraries.patch"
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -r ${_realname}-${pkgver}-source build-${MSYSTEM} && cd build-${MSYSTEM}

  make USE_SYSTEM_LIBS=yes HAVE_GLUT=yes HAVE_WIN32=yes WINDRES=windres prefix=${MINGW_PREFIX} \
    DESTDIR=install build=release libs apps install
}

package_libmupdf() {
  pkgdesc='Library for Lightweight PDF and XPS viewer (mingw-w64)'
  depends=()
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-libmupdf")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-libmupdf")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-libmupdf")

  cd build-${MSYSTEM}

  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/libmupdf" README COPYING CHANGES
  cp -r install${MINGW_PREFIX}/{include,lib} "${pkgdir}${MINGW_PREFIX}/"
}

package_mupdf() {
  pkgdesc='Lightweight PDF and XPS viewer (mingw-w64)'
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf")

  cd build-${MSYSTEM}

  install -D -m0755 install${MINGW_PREFIX}/bin/mupdf-w32 "${pkgdir}${MINGW_PREFIX}/bin/mupdf.exe"
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf" README COPYING CHANGES
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" install${MINGW_PREFIX}/share/man/man1/mupdf.1*
  install -d -m0755 "${pkdir}${MINGW_PREFIX}/share/doc"
  cp -r install${MINGW_PREFIX}/share/doc/mupdf "${pkgdir}${MINGW_PREFIX}/share/doc/"
}

package_mupdf-gl() {
  pkgdesc='Lightweight PDF and XPS viewer with OpenGL backend (mingw-w64)'
  depends+=("${MINGW_PACKAGE_PREFIX}-freeglut")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-gl")
  conflicts=("${MINGW_PACKAGE_PREFIX}-mupdf"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-gl")
  provides=("${MINGW_PACKAGE_PREFIX}-mupdf=${pkgver}"
            "${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-gl")

  cd build-${MSYSTEM}

  install -D -m0755 install${MINGW_PREFIX}/bin/mupdf-gl "${pkgdir}${MINGW_PREFIX}/bin/mupdf.exe"
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf" README COPYING CHANGES
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" install${MINGW_PREFIX}/share/man/man1/mupdf.1*
  install -d -m0755 "${pkdir}${MINGW_PREFIX}/share/doc"
  cp -r install${MINGW_PREFIX}/share/doc/mupdf "${pkgdir}${MINGW_PREFIX}/share/doc/"
}

package_mupdf-tools() {
  pkgdesc='Tools for Lightweight PDF and XPS viewer (mingw-w64)'
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-tools")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-tools")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-tools")

  cd build-${MSYSTEM}

  install -D -m0755 -t "${pkgdir}${MINGW_PREFIX}/bin" install${MINGW_PREFIX}/bin/mu{raster,tool}
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf-tools" README COPYING CHANGES
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" install${MINGW_PREFIX}/share/man/man1/mutool.1*
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
